﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using TravelLib;

namespace DatasourceParser
{
    public class Parser
    {
     
        XmlDocument xDoc = new XmlDocument();
        

        public int index;
        public string airName,airCode;


        public void Setup()
        {
            xDoc.Load(@"Resources\travelXML.xml"); //File is located in the "\bin\Debug\Resources" folder
            XmlNodeList companiesList = xDoc.SelectNodes("travel/child::node()");
            TravelClassEnum eco = TravelClassEnum.BUSSINESS_CLASS;
            TravelClassEnum bus = TravelClassEnum.BUSSINESS_CLASS;
            foreach (XmlNode co in companiesList)
            {
                
                //store company info
                Company myComp = new Company();
                myComp.CompanyName = co["name"].InnerText;
                myComp.Flights = new List<Flight>();
                XmlNodeList flightsList = co.SelectNodes("flights/child::node()");
                foreach (XmlNode fo in flightsList)
                {
                    //XmlNode fnode = fo.SelectSingleNode("flight");
                    if (fo!= null)
                    {
                        //store flight info
                        Flight myFlight = new Flight();
                        FlightClass myClass1 = new FlightClass();
                        FlightClass myClass2 = new FlightClass();
                        XmlNode fc= fo.SelectSingleNode("seats");
                        myFlight.UID = fo["flightID"].InnerText;                        
                        myFlight.Company = myComp;
                        myFlight.DepartureTime = DateTime.Parse(fo["dtefrom"].InnerText);
                        myFlight.ArrivalTime = DateTime.Parse(fo["dteto"].InnerText);
                        myFlight.UnitCost = Int32.Parse(fo["units"].InnerText);
                        myFlight.Route = new Route(fo["namedep"].InnerText, fo["namearr"].InnerText, myFlight.DepartureTime, myFlight.ArrivalTime);
                        myClass1.TotalSeats = Int32.Parse(fc["business"].InnerText);
                        myClass1.SeatsAvailable = Int32.Parse(fc["business"].InnerText);
                        myClass2.SeatsAvailable = Int32.Parse(fc["economy"].InnerText);
                        myClass2.TotalSeats = Int32.Parse(fc["economy"].InnerText);
                        myClass1.TravelClass= bus;                    
                        myFlight.TravelClasses.Add(myClass1);
                        myClass2.TravelClass = eco;
                        myFlight.TravelClasses.Add(myClass2);
                        myFlight.Company = myComp;
                        myComp.Flights.Add(myFlight);
                    }
                }
                CompanyManager.Companies.Add(myComp);
            }
        }

    }
}
